package com.nanychuan.blogback.controller;

import com.nanychuan.blogback.pojo.User;
import com.nanychuan.blogback.service.UserService;
import com.nanychuan.blogbase.exception.BlogException;
import com.nanychuan.blogbase.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 往
 * 管理员后台登录
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/blog/userLogin")
    @ResponseBody
    public Result login(User user, String code, HttpSession session){
        //创建返回结果对象
        Result result = new Result();
        //中session中获取验证码
        String rightCode = (String) session.getAttribute("code");
        //判断
        try{
            user = userService.login(user,code,rightCode);
            result.setFlag(true);
            //把登录成功的用户存放到session中，显示在登录首页
            session.setAttribute("user",user);

        }catch (BlogException e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    //用户登录成功跳转到后台首页

    @RequestMapping("/workbench/index")
    public String toIndex(){
        return "workbench/index";
    }

    //退出登录

    @RequestMapping("/user/loginOut")
    public String loginOut(HttpSession session){
        //清楚session中的对象
        session.removeAttribute("user");
        //重定向到登录界面
        return "redirect:/login.jsp";
    }

    //异步校验用户输入的原始密码是否正确

    @RequestMapping("/blog/verifyOldPassword")
    @ResponseBody
    public Result verifyOldPassword(String oldPassword,HttpSession session){
        Result result = new Result();
        try{
            //获取当前登录的用户
            User user = (User) session.getAttribute("user");
            userService.verifyOldPwd(oldPassword,user);
            result.setFlag(true);
        }catch (BlogException e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    //异步校验用户信息

    @RequestMapping("/blog/updateUser")
    @ResponseBody
    public Result updateUser(User user,HttpSession session){
        Result result = new Result();
        try {
            userService.updateUser(user);
            //清楚session中的数据
            session.removeAttribute("user");
            result.setFlag(true);
            result.setMessage("修改用户信息成功");
        }catch (BlogException e)
        {
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    //博客前台登录
    @RequestMapping("/blog/blogLogin")
    @ResponseBody
    public Result blogLogin(User user){
        //创建返回对象
        Result result = new Result();
        //判断用户是否存在
        try{
            userService.blogLogin(user);
            result.setFlag(true);
        }catch (BlogException b){
            result.setMessage(b.getMessage());
        }
        return result;

    }
    @RequestMapping("/blogreception/BlogIndex")
    public String blogIndex(){
        return "blogreception/BlogIndex";
    }
}
